package naga.x.platform2.app.model;

import java.util.Arrays;
import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;

import naga.x.platform2.app.Channel;
import naga.x.platform2.utils.StringUtils;

/**
 * 游戏服务器信息
 * @author yang.li
 *
 */
public class GameServer implements Comparable<GameServer> {

	/** 游戏服id */
	private int id;
	/** 游戏服名称 */
	private String name;
	/** 游戏服所属渠道 */
	private Channel[] channels;
	/** 游戏服域名/ip */
	private String host;
	/** 游戏服ip */
	private String ip;
	/** 游戏服连接端口 */
	private int port;
	/** 游戏服http端口（充值回调、GM） */
	private int httpPort;
	/** 秘钥 */
	private String key;
	/** 游戏服状态 */
	private State state;
	/** 权重（服务器列表该值越大位置越靠前） */
	private int weight;
	/** 代理连接端口 */
	private int[] proxyPorts;
	/** 是否开启ssl */
	private boolean ssl;
	/** 开服时间 */
	private Date time;
	/** 是否为测试服 */
	private boolean test;
	
	private int tick;
	
	public static enum State {
		MAINTAIN("维护"),
		NEW("新开"),
		FLUENT("流畅"),
		NORMAL("普通"),
		CROWD("拥挤"),
		FIRE("火爆"),
		;
		public final String desc;
		State(String desc) {
			this.desc = desc;
		}
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@JSONField(serialize = false)
	public Channel[] getChannels() {
		return channels;
	}

	public void setChannels(Channel[] channels) {
		this.channels = channels;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@JSONField(name = "ip")
	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}
	
	@JSONField(serialize = false)
	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getPort() {
		int[] ports = proxyPorts;
		if (ports != null && ports.length > 0) {
			int i = tick++;
			if (i >= ports.length) { // maybe exceed when concurrent
				i = 0;
			}
			if (tick >= ports.length) {
				tick = 0;
			}
			return ports[i];
		}
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	@JSONField(serialize = false)
	public int getHttpPort() {
		return httpPort;
	}

	public void setHttpPort(int httpPort) {
		this.httpPort = httpPort;
	}

	@JSONField(serialize = false)
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	@JSONField(serialize = false)
	public State getState() {
		return state;
	}

	public void setState(State state) {
		this.state = state;
	}

	@JSONField(serialize = false)
	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}
	
	@JSONField(serialize = false)
	public int[] getProxyPorts() {
		return proxyPorts;
	}

	public void setProxyPorts(int[] proxyPorts) {
		this.proxyPorts = proxyPorts;
	}
	
	public boolean isSsl() {
		return ssl;
	}

	public void setSsl(boolean ssl) {
		this.ssl = ssl;
	}

	@JSONField(serialize = false)
	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	@JSONField(serialize = false)
	public boolean isTest() {
		return test;
	}

	public void setTest(boolean test) {
		this.test = test;
	}

	public String getTips() {
		return state != null ? state.desc : "";
	}
	
	private String payCallbackUrl;
	@JSONField(serialize = false)
	public String getPayCallbackUrl() {
		if (payCallbackUrl == null) {
			payCallbackUrl = "http://" + getIp() + ":" + getHttpPort() + "/recharge";
		}
		return payCallbackUrl;
	}
	
	private String gmUrl;
	@JSONField(serialize = false)
	public String getGMUrl() {
		if (gmUrl == null) {
			gmUrl = "http://" + getHost() + ":" + getHttpPort() + "/xgm";
		}
		return gmUrl;
	}

	@Override
	public int compareTo(GameServer o) {
		return o.weight - weight;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!(obj instanceof GameServer)) {
			return false;
		}
		GameServer other = (GameServer) obj;
		if (id != other.id) {
			return false;
		}
		if (!StringUtils.maskNull(name).equals(other.name)) {
			return false;
		}
		if (!Arrays.equals(channels, other.channels)) {
			return false;
		}
		if (!StringUtils.maskNull(host).equals(other.host)) {
			return false;
		}
		if (!StringUtils.maskNull(ip).equals(other.ip)) {
			return false;
		}
		if (port != other.port) {
			return false;
		}
		if (httpPort != other.httpPort) {
			return false;
		}
		if (!StringUtils.maskNull(key).equals(other.key)) {
			return false;
		}
		if (state != other.state) {
			return false;
		}
		if (weight != other.weight) {
			return false;
		}
		if (!Arrays.equals(proxyPorts, other.proxyPorts)) {
			return false;
		}
		if (ssl != other.ssl) {
			return false;
		}
		long t1 = time != null ? time.getTime() : 0L;
		long t2 = other.time != null ? other.time.getTime() : 0L;
		if (t1 != t2) {
			return false;
		}
		return true;
	}
	
}
